package com.baizhi.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {
    private static final Logger log = LoggerFactory.getLogger(DemoController.class);

    @GetMapping("demo")
    // @HystrixCommand(fallbackMethod = "demoFallback")   // 熔断之后的处理
    @HystrixCommand(defaultFallback = "defaultFallback")
    public String demo(Integer id) {
        log.info("demo server ...");
        if (id <= 0) {
            throw new RuntimeException("无效id!!!");
        }
        return "demo ok !!!";
    }

    @GetMapping("demo1")
    @HystrixCommand(defaultFallback = "defaultFallback")
    public String demo1() {
        System.out.println("demo1 ok !!!");
        return "demo1 ok !!!";
    }

    // 默认的备选处理
    public String defaultFallback() {
        return "网络连接失败，请重试！！！";
    }

    // 自定义的备选处理
    public String demoFallback(Integer id) {
        return "当前活动过于火爆，服务已经被熔断了！！！";
    }
}
